package com.itheima.udpdeom;

import java.net.*;
import java.util.Scanner;

public class UDPClientDemo {

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("客户端启动...");
        // 创建一个客户端对象，注册端口
        DatagramSocket socket = new DatagramSocket();
        // 创建数据报对象，封装要发送的数据
        byte[] bytes = "我是客户端，正在发送消息".getBytes();
        DatagramPacket packet;
        // 循环发送数据
        while (true) {
            System.out.println("请输入要发送的内容：");
            String msg = scanner.nextLine();
            if ("exit".equals(msg)) {
                socket.close();
                break;
            }
            bytes = msg.getBytes();
            packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8080);
            socket.send(packet);
        }
    }
}
